//
//  AuditGeneralViewController.m
//  DigiOps
//
//  Created by app on 2018/7/24.
//  Copyright © 2018年 俞渊华. All rights reserved.
//

#import "AuditGeneralViewController.h"
#import "AuditViewCell.h"
#import "DetailViewController.h"
#import "AuditScoreViewController.h"
#import "SpotCreateViewCell.h"
#import "WBPopMenuModel.h"
#import "WBPopMenuSingleton.h"
#import "WBPopMenuView.h"
#import "AuditFinalViewCell.h"
#import "UserSites.h"
#import "AuditCategory.h"
#import "PointModel.h"
#import "PointModelOffline.h"
#import "PointValueModel.h"
#import "SopNotokViewController.h"

@interface AuditGeneralViewController ()
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *sopArray;

@property(nonatomic, assign)NSInteger pageSize;
@property(nonatomic, assign)NSInteger pageIndex;
@property(nonatomic, strong)NSMutableArray *scoreArray;
@property(nonatomic, strong)NSMutableArray *titleArray;
@property(nonatomic, strong)NSMutableArray *finaTitleArray;
@property(nonatomic,strong)NSString* categoryName;
@property(nonatomic,strong)NSString* categoryWeight;
@property(nonatomic, strong)NSString* auditUuid;
//@property(nonatomic,assign)double categoryHigh;
//评价按钮
@property(nonatomic, strong)UIButton* auditorbutton;

@end

@implementation AuditGeneralViewController
static AuditGeneralViewController *agvc = nil;

+ (instancetype)share {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        agvc = [[self alloc] init];
    });
    return agvc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageSize = 20;
    self.pageIndex = 1;
    self.sopArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    
    CGRect labelFrame = CGRectMake(0.0, 0.0, SCREEN_WIDTH, 36.0);
    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
    label.font = [UIFont boldSystemFontOfSize:18];
    label.numberOfLines = 2;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentLeft;
    label.textColor = [UIColor whiteColor];
    //    label.shadowColor = [UIColor blackColor];
    //    label.shadowOffset = CGSizeMake(0.0, -1.0);
    label.lineBreakMode = UILineBreakModeMiddleTruncation;
    label.text=@"审查";
    self.navigationItem.titleView = label;
    
    self.scoreArray=[NSMutableArray new];
    self.titleArray=[[NSMutableArray alloc] initWithCapacity:100];
    self.finaTitleArray=[NSMutableArray new];
//    self.scoreArray=@[@"1.1门窗已关闭门窗已关闭...",@"1.2门窗已关闭门窗已关闭...",@"1.2门窗已关闭门窗已关闭...",@"1.2门窗已关闭门窗已关闭...",@"1.2门窗已关闭门窗已关闭...",@"1.2门窗已关闭门窗已关闭...",@"1.2门窗已关闭门窗已关闭..."];
    //    self.titleArray=@[@"1.选检查点",@"2.选优先级",@"3.选分配者",@"4.开始时间",@""];
//    self.titleArray=[NSMutableArray arrayWithObjects:@"打分",@"打分",@"打分",@"打分",@"打分",@"打分",@"打分", nil];
    for (int i=0; i<100; i++) {
        [self.titleArray addObject:@"打分"];
    }
    self.finaTitleArray=@[@"审查_通用当前得分:",@"分类最高可得分:",@"整体审查成绩:"];
    UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleBordered target:self action:@selector(clickEvent)];
    [myButton setTintColor:[UIColor whiteColor]];
    //去除提交
//    self.navigationItem.rightBarButtonItem = myButton;
    
    /** MJRefresh的block也需要用若引用 */
    WEAKSELF
//    self.tableView.mj_header = [XMRefreshHeader xm_headerWithRefreshingBlock:^{
//        self.pageIndex = 1;
//        [self.sopArray removeAllObjects];
//        //        [weakSelf searchAticle];
//    }];
    //默认block方法：设置上拉加载更多
    //调本地存储
    NSArray * array = [PointModel findWhere:@[@"uuid",@"=",_ordermodel.uuid]];
    
    NSMutableArray* Temp=[NSMutableArray new];
    for (PointModel* model in array) {
        if ([model.categoryName isEqualToString:self.index]) {
            [Temp addObject:model];
        }
        
    }
    if (Temp.count>0) {
        [self.scoreArray removeAllObjects];
        [self.scoreArray addObjectsFromArray:Temp];
        
        //排序
        NSArray *sortArray = [self.scoreArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            
            PointModel *pModel1 = obj1;
            PointModel *pModel2 = obj2;
            if ([pModel1.seq intValue] > [pModel2.seq intValue]) { //不使用intValue比较无效
                return NSOrderedDescending;//降序
            }else if ([pModel1.seq intValue] < [pModel2.seq intValue]){
                return NSOrderedAscending;//升序
            }else {
                return NSOrderedSame;//相等
            }
            
        }];
        [self.scoreArray removeAllObjects];
        self.scoreArray=[NSMutableArray arrayWithArray:sortArray];
        
        //赋值sopuuid
        for (PointModel* pm in Temp) {
            _auditUuid=pm.auditUuid;
            break;
        }
        
        [self.tableView reloadData];
    }
    else
    {
        //离线存储
        NSArray * array1 = [PointModelOffline findWhere:@[@"offlineuuid",@"=",_ordermodel.orderTypeUuid]];
        NSMutableArray* Temp1=[NSMutableArray new];
        for (PointModelOffline* model in array1) {
            if ([model.categoryName isEqualToString:self.index]) {
                PointModel* aa=[PointModel new];
                aa.seq=model.seq;
                aa.title=model.title;
                aa.content=model.content;
                aa.shiftUuid=model.shiftUuid;
                aa.sopUuid=model.sopUuid;
                aa.auditUuid=model.auditUuid;
                aa.categoryName=model.categoryName;
                aa.categoryWeight=model.categoryWeight;
                aa.type=model.type;
                aa.valueArr=model.valueArr;
                aa.picture=model.picture;
                aa.remark=model.remark;
                aa.uuid=_ordermodel.uuid;//
                aa.pointNum=model.pointNum;
                
                [Temp1 addObject:aa];
            }
            
        }
        if (Temp1.count>0) {
            [self.scoreArray removeAllObjects];
            [self.scoreArray addObjectsFromArray:Temp1];
            
            //排序
            NSArray *sortArray = [self.scoreArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
                
                PointModel *pModel1 = obj1;
                PointModel *pModel2 = obj2;
                if ([pModel1.seq intValue] > [pModel2.seq intValue]) { //不使用intValue比较无效
                    return NSOrderedDescending;//降序
                }else if ([pModel1.seq intValue] < [pModel2.seq intValue]){
                    return NSOrderedAscending;//升序
                }else {
                    return NSOrderedSame;//相等
                }
                
            }];
            [self.scoreArray removeAllObjects];
            self.scoreArray=[NSMutableArray arrayWithArray:sortArray];
            
            //赋值sopuuid
            for (PointModel* pm in Temp1) {
                _auditUuid=pm.auditUuid;
                break;
            }
            
            //分类最高分 分数平均分*比重*10
            double s=0;
            double pp=0;
            double o=0;
            for (PointModel* pm in self.scoreArray) {
                if ([pm.content hasPrefix:@"["]) {
                    if (pm.valueArr.count>0)
                    {
                        PointValueModel* p=(PointValueModel*)pm.valueArr[pm.valueArr.count-1];
                        o=[p.value doubleValue];
                    }
                }
                //            double o=[pm.content doubleValue];
                double p=[pm.categoryWeight doubleValue];
                //            s=s+o*p/100;
                pp=p;
                s=s+o;
            }
            s=s/self.scoreArray.count*pp/100;
            if(s>0.2)//临时写死
                s=0.2;
            if (s==0.13333333333333333) {
                s=0.2;
            }
            if (s>0) {
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                NSString *token = [NSString stringWithFormat: @"%@High",self.index];
                [defaults setDouble:s*100 forKey:token];
                [defaults synchronize];
            }
            
            [self.tableView reloadData];
        }
        else{
    [self postAuditUI];
        }
    }
    
    //返回
    self.navigationItem.hidesBackButton = YES;
    UIImage *image = [UIImage imageNamed:@"Back"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 30, 44);
    backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [backButton setImage:image forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftItemsSupplementBackButton = YES;
}
-(void)goBack
{

    //执行代理方法
    if ([_delegate respondsToSelector:@selector(AuditTranfer:pointNum:)]) {
        [_delegate AuditTranfer:self.scoreArray pointNum:self.categoryName];
    }
    [self.rt_navigationController popViewControllerAnimated: YES];
    NSArray * array = [PointModel findWhere:@[@"uuid",@"=",_ordermodel.uuid]];
    if (array.count>0) {
        //删除sopuuid的pointmodel
        for (PointModel* pm in array) {
            [PointModel deleteWhere:@[@"uuid",@"=",_ordermodel.uuid,@"categoryName",@"=",self.index]];
        }
    }
    for (PointModel* pm in self.scoreArray) {
        [pm saveOrUpdateAsync:^(BOOL isSuccess) {
            
        }];
    }
}
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
}
-(void)viewWillDisappear:(BOOL)animated{
    if (self.block) {
        self.block(self.scoreArray);
    }
}

-(void)searchAticle{
    
}
-(void)postAuditUI{
    UserSites* s=[UserSites mj_objectWithKeyValues:[UserInfo share].sites[0]];
    
    NSDictionary *params = @{
                             @"siteUuid" : s.uuid
                             };
    [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    [HttpTool get:[NSString stringWithFormat:@"digiops/app/SelectAudits"] params:nil success:^(id responseObj) {
        NSLog(@"");
        [self.sopArray removeAllObjects];
        for (NSDictionary* audit in responseObj[@"content"]) {
            AuditModel* a=[AuditModel mj_objectWithKeyValues:audit];

            if ([_model.uuid isEqualToString:a.uuid]) {

//                self.titleLabelb.text=a.shiftName;
                for (NSDictionary* template in a.category) {
                    AuditCategory* cat=[AuditCategory mj_objectWithKeyValues:template];
                    for (NSDictionary* category in cat.category) {

                    PointModel* p=[PointModel mj_objectWithKeyValues:category];
                        p.auditUuid=a.uuid;
                        if (p.content) {
                           
                    NSData *data = [p.content dataUsingEncoding:NSUTF8StringEncoding];
                    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                    NSMutableArray* arr=[NSMutableArray new];
                    for (NSDictionary* v in dict) {
                        PointValueModel* model=[PointValueModel mj_objectWithKeyValues:v];
                        [arr addObject:model];
                        
                    }
                    p.valueArr=arr;
                        }
                    NSLog(@"");
                        _auditUuid=a.uuid;
                        p.auditUuid=a.uuid;
                        p.uuid=_ordermodel.uuid;
                        //区分选择了哪个审计
                    if ([self.index isEqualToString:cat.categoryName])
                        [self.scoreArray addObject:p];
                            
                    }
                }
            }
            NSLog(@"");

        }
        //离线存储
        for (NSDictionary* audit in responseObj[@"content"]) {
            AuditModel* a=[AuditModel mj_objectWithKeyValues:audit];

            for (NSDictionary* template in a.category) {
                AuditCategory* cat=[AuditCategory mj_objectWithKeyValues:template];
                for (NSDictionary* category in cat.category) {
                PointModelOffline* p=[PointModelOffline mj_objectWithKeyValues:category];
                
                    p.auditUuid=a.uuid;
                    if (p.content) {
                        
                        NSData *data = [p.content dataUsingEncoding:NSUTF8StringEncoding];
                        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                        NSMutableArray* arr=[NSMutableArray new];
                        for (NSDictionary* v in dict) {
                            PointValueModel* model=[PointValueModel mj_objectWithKeyValues:v];
                            [arr addObject:model];
                            
                        }
                        p.valueArr=arr;
                    }
                    
                    _auditUuid=a.uuid;
                    p.auditUuid=a.uuid;
                    p.offlineuuid=a.uuid;
                    p.pointNum=a.uuid;

                [p saveOrUpdateAsync:^(BOOL isSuccess) {
                    
                }];
                NSLog(@"");
                }
            }
            NSLog(@"");
            
        }
        
        NSLog(@"");
        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
        [self.tableView reloadData];
        
        //分类最高分 分数平均分*比重*10
        double s=0;
        double pp=0;
        double o=0;
        for (PointModel* pm in self.scoreArray) {
            if ([pm.content hasPrefix:@"["]) {
                if (pm.valueArr.count>0)
                {
                    PointValueModel* p=(PointValueModel*)pm.valueArr[pm.valueArr.count-1];
                    o=[p.value doubleValue];
                }
            }
            //            double o=[pm.content doubleValue];
            double p=[pm.categoryWeight doubleValue];
            //            s=s+o*p/100;
            pp=p;
            s=s+o;
        }
        s=s/self.scoreArray.count*pp/100;
        if (s>0) {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            NSString *token = [NSString stringWithFormat: @"%@High",self.index];
            [defaults setDouble:s*100 forKey:token];
            [defaults synchronize];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
        //        [UITool showMBProgressHud:[UIApplication sharedApplication].keyWindow withText:@"登录失败，稍后再试" withTime:1.0];
        
        //        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
        
    }];
}
-(void)clickEvent{
    NSMutableArray *obj = [NSMutableArray array];
    
    for (NSInteger i = 0; i < [self titles].count; i++) {
        
        WBPopMenuModel * info = [WBPopMenuModel new];
        //        info.image = [self images][i];
        info.title = [self titles][i];
        [obj addObject:info];
    }
    
    [[WBPopMenuSingleton shareManager]showPopMenuSelecteWithFrame:150
                                                             item:obj
                                                           action:^(NSInteger index) {
                                                               NSLog(@"index:%ld",(long)index);
                                                               [self submit];
                                                           }];
}
- (NSArray *) titles {
    return @[@"提交"];
}

- (NSArray *) images {
    return @[@"right_menu_QR@3x",
             @"right_menu_addFri@3x",
             @"right_menu_multichat@3x",
             @"right_menu_sendFile@3x",
             @"right_menu_facetoface@3x",
             @"right_menu_payMoney@3x"];
}
-(void)submit
{
    for (PointModel* pm in self.scoreArray) {
        if ([pm.content hasPrefix:@"["]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"没有打分" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            return;
        }
        
    }

    //执行代理方法
    if ([_delegate respondsToSelector:@selector(AuditTranfer:pointNum:)]) {
        [_delegate AuditTranfer:self.scoreArray pointNum:self.categoryName];
    }
    [self.rt_navigationController popViewControllerAnimated: YES];
    NSArray * array = [PointModel findWhere:@[@"uuid",@"=",_ordermodel.uuid]];
    if (array.count>0) {
        //删除sopuuid的pointmodel
        for (PointModel* pm in array) {
            [PointModel deleteWhere:@[@"uuid",@"=",_ordermodel.uuid,@"categoryName",@"=",self.index]];
        }
    }
    for (PointModel* pm in self.scoreArray) {
        [pm saveOrUpdateAsync:^(BOOL isSuccess) {
            
        }];
    }
}
-(void)auditorComment
{
    SopNotokViewController* detailVC = [[SopNotokViewController alloc]init];
    //    detailVC.model = self.aticleArray[indexPath.row];
//    detailVC.model=model;
    detailVC.shift=NO;
    [self.rt_navigationController pushViewController:detailVC animated:YES];
    detailVC.block = ^(NSString *txt) {
        NSMutableArray* sopTemp=[NSMutableArray new];
//        for (PointModel* model in self.sopArray) {
//            if ([model.seq isEqualToString:self.seq]) {
//                //已存数据库备注为空问题
//                if (txt.length>0)
//                {
//                    model.remark=txt;
//                    model.content=@"不正常";
//                }
//            }
//            [sopTemp addObject:model];
//        }
//        [self.sopArray removeAllObjects];
//        self.sopArray=sopTemp;
        [self.auditorbutton setTitleColor:[UIColor xm_jetblackColor] forState:UIControlStateNormal];

    };
    detailVC.blockPic = ^(NSString *txt) {
        NSMutableArray* sopTemp=[NSMutableArray new];
//        for (PointModel* model in self.sopArray) {
//            if ([model.seq isEqualToString:self.seq]) {
//                //已存数据库图片为空问题
//                if (txt.length>1)
//                    model.picture=txt;
//                model.content=@"不正常";
//            }
//            [sopTemp addObject:model];
//        }
//        [self.sopArray removeAllObjects];
//        self.sopArray=sopTemp;
    };
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.scoreArray.count>0) {
        return self.scoreArray.count+3;
    }
    else
    {
        return 0;
    }
//    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row>self.scoreArray.count-1) {
        if(indexPath.row==self.scoreArray.count+1)
        {
            AuditFinalViewCell* cell=[AuditFinalViewCell cellWithTableView:tableView];
//            cell.auditFinal.text=[self.finaTitleArray objectAtIndex:indexPath.row-self.scoreArray.count];
            cell.auditFinal.text=[NSString stringWithFormat:@"%@最高可得分：",self.index];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;

            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            double password = [defaults doubleForKey:[NSString stringWithFormat: @"%@High",self.index]];
            if (password) {
                [cell.scoreBar setTitle:[NSString stringWithFormat:@"%.2f",password] forState:UIControlStateNormal];

            }
            //        double d=
            return cell;
        }
        else if(indexPath.row==self.scoreArray.count+2)
        {
            AuditFinalViewCell* cell=[AuditFinalViewCell cellWithTableView:tableView];
            cell.auditFinal.text=[self.finaTitleArray objectAtIndex:indexPath.row-self.scoreArray.count];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;

            //整体成绩
            [cell.scoreBar setTitle:[MySingleton sharedMySingleton].finalscore forState:UIControlStateNormal];
            //        double d=
            return cell;
        }
        else
        {
        AuditFinalViewCell* cell=[AuditFinalViewCell cellWithTableView:tableView];
        cell.auditFinal.text=[NSString stringWithFormat:@"%@得分：",self.index];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

        //当前总得分 分数平均分*比重*10
        double s=0.00;
        double pp=0.00;
        double high=0.00;
        for (PointModel* pm in self.scoreArray) {
            double o=[pm.content doubleValue];
            double p=[pm.categoryWeight doubleValue];
            PointValueModel* pvm=(PointValueModel*)pm.valueArr[0];
            double h=[pvm.value doubleValue];
//            s=s+o*p/100;
            pp=p;
            s=s+o;
            high=high+h;
        }
        s=s/high*pp/10.00;
        [cell.scoreBar setTitle:[NSString stringWithFormat:@"%.2f",s*10.00] forState:UIControlStateNormal];
//        double d=
        return cell;
        }
    }
    else{
    SpotCreateViewCell* cell=[SpotCreateViewCell cellWithTableView:tableView];
        PointModel* model=(PointModel*)[self.scoreArray objectAtIndex:indexPath.row];
        cell.spotTitle.text=[NSString stringWithFormat:@"%@.%@",model.seq, model.title];
        //自动折行设置
        cell.spotTitle.lineBreakMode = UILineBreakModeWordWrap;
        cell.spotTitle.numberOfLines = 0;
        
    cell.spotTitle.font=[UIFont xm_medium:15.0];
    cell.spotContent.text=[self.titleArray objectAtIndex:indexPath.row];
        //同步区分
        if ([model.categoryName isEqualToString:self.index]) {
            if (model.content)
            {
                if ([model.content hasPrefix:@"["]) {
                    if (model.valueArr.count>0)
                    {
                        PointValueModel* p=(PointValueModel*)model.valueArr[0];
                        cell.spotContent.text=p.value;
                        //
                        cell.spotContent.text=@"打分";
                    }
                }
                else
                    cell.spotContent.text=model.content;
                
            }
        }
    
    cell.block = ^(UIButton *sender) {
        AuditScoreViewController * detailVC = [[AuditScoreViewController alloc]init];
            detailVC.model = model;
        [self.rt_navigationController pushViewController:detailVC animated:YES];
        detailVC.block = ^(NSString *score,NSString* seqa) {
            cell.spotContent.text=score;

            NSMutableArray* sopTemp=[NSMutableArray new];
            for (PointModel* pm in self.scoreArray) {
                
            if ([pm.seq isEqualToString:seqa]) {
                pm.content=score;
                self.categoryName=pm.categoryName;
                self.categoryWeight=pm.categoryWeight;
            }
                [sopTemp addObject:pm];
            }
            [self.scoreArray removeAllObjects];
            self.scoreArray=sopTemp;
            //分数显示
            [self.titleArray replaceObjectAtIndex:indexPath.row withObject:score];
//            NSMutableArray* titleTemp=[NSMutableArray new];
//            for (int i=0;i<self.titleArray.count; i++) {
//                NSString* pm=[self.titleArray objectAtIndex:i];
//                if (i==indexPath.row) {
//                    pm=score;
//                }
//                [titleTemp addObject:pm];
//            }
//            [self.titleArray removeAllObjects];
//            self.titleArray=titleTemp;
            
            [self.tableView reloadData];
        };
    };
//    AuditViewCell* cell=[AuditViewCell cellWithTableView:tableView];
    //    cell.model=[RouteModel new];
    return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row>self.scoreArray.count-1) {
        return 80;
    }
    else{
    return 130;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    /*
     NewsDetailViewController * detailVC = [[NewsDetailViewController alloc]init];
     detailVC.model = self.aticleArray[indexPath.row];
     [self.rt_navigationController pushViewController:detailVC animated:YES];
     */
    if (indexPath.row<self.scoreArray.count+1) {

    AuditScoreViewController * detailVC = [[AuditScoreViewController alloc]init];
    //    detailVC.model = self.aticleArray[indexPath.row];
    [self.rt_navigationController pushViewController:detailVC animated:YES];
    }
}


- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [UIView new];
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        view.backgroundColor = XMColor(241, 245, 247);
        UILabel* titleLabel = [[UILabel alloc]init];
        titleLabel.font = [UIFont xm_medium:17.0];
        titleLabel.numberOfLines = 1;
        titleLabel.textColor = [UIColor xm_jetblackColor];
        titleLabel.text=self.index;
        
        [view addSubview:titleLabel];
        tableView.tableHeaderView=view;
        
//        UIView * viewFoot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
//        viewFoot.backgroundColor = [UIColor whiteColor];
//        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
//        //        button.frame = CGRectMake(40, 100, SCREEN_WIDTH- 80, 30);
//        button.backgroundColor = DGColor;
//        [button setTitle:@"提交" forState:UIControlStateNormal];
//        [button addTarget:self action:@selector(submitRoute) forControlEvents:UIControlEventTouchUpInside];
//        [viewFoot addSubview:button];
//        tableView.tableFooterView=viewFoot;
        //        [button setImage:[UIImage imageNamed:@"image"] forState:UIControlStateNormal];
        //        [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        //        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view).offset(18);
            make.left.equalTo(view.mas_left).offset(20);
        }];
        
//        [button mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.center.equalTo(viewFoot).offset(0);
//            //            make.top.equalTo(view).offset(5);
//            make.width.mas_equalTo(SCREEN_WIDTH-80);
//            make.height.mas_equalTo(50);
//            //            make.left.equalTo(view.mas_left).offset(20);
//        }];
        _tableView = tableView;
    }
    return _tableView;
}
-(void)submitRoute
{
    
}
+(NSString*) removeLastOneChar:(NSString*)origin
{
    NSString* cutted;
    if([origin length] > 0){
        cutted = [origin substringToIndex:([origin length]-1)];// 去掉最后一个","
    }else{
        cutted = origin;
    }
    return cutted;
}


@end
